
library(foreign)


setwd("/Users/davidcarter/Dropbox/Systemic Crisis Rep/")


claims<-read.csv("claims_data.csv")

claims<-claims[claims$region==2,]

#yr_claims<-read.dta("claims_data.dta" )


#setwd("~/Dropbox/Systemic Crisis Rep")


majors<-read.csv(file="majors2016.csv")


#setwd("~/Dropbox/David Paper/")


yr_claims<-read.csv("claims_panel.csv" )

yr_claims<-yr_claims[yr_claims$region==2,]


claims$tgt_gp<-0


claims$chal_gp<-0

claims$year<-as.numeric(substr(claims$begclaim,1,4))


for(i in 1:length(majors)){
	
	
	claims$tgt_gp<-ifelse(claims$tgt ==     majors[i,]$ccode & ( claims$year >= majors[i,]$styear & claims$year <= majors[i,]$endyear),1,claims$tgt_gp)
	
	claims$chal_gp<-ifelse(claims$chal ==     majors[i,]$ccode & ( claims$year >= majors[i,]$styear & claims$year <= majors[i,]$endyear),1,claims$chal_gp)
	
	
	
}



claims$type<-0

claims$type<-ifelse(claims$tgt_gp==1 & claims$chal_gp==1, 3,claims$type)
claims$type<-ifelse(claims$tgt_gp==0 & claims$chal_gp==1, 2,claims$type)
claims$type<-ifelse(claims$tgt_gp==1 & claims$chal_gp==0, 1,claims$type)


table(claims$tgt_gp,claims$chal_gp)
prop.table(table(claims$tgt_gp,claims$chal_gp))


no_gp<-tapply(claims$tgt_gp==0& claims$chal_gp==0 ,claims$year,sum )

tar_gp<-tapply(claims$tgt_gp==1& claims$chal_gp==0 ,claims$year,sum )

chal_gp<-tapply(claims$tgt_gp==0& claims$chal_gp==1 ,claims$year,sum )

both_gp<-tapply(claims$tgt_gp==0& claims$chal_gp==1 ,claims$year,sum )


barplot(both_gp+chal_gp+tar_gp+no_gp,col="black",ylim=c(0,35))
grid()
par(new=T)
barplot(chal_gp+tar_gp+no_gp,col="dark grey",ylim=c(0,35),axes=F,xlab="",names.arg=F)
par(new=T)
barplot(tar_gp+no_gp,col="light grey",ylim=c(0,35),axes=F,xlab="",names.arg=F)

par(new=T)
barplot(no_gp,ylim=c(0,35),axes=F,xlab="",names.arg=F,density=10,col="black",angle=180)


legend("topright", legend=c("No GP","Target GP", "Challenger GP","Both GP"), col=c("black","light grey","dark grey","black"),lty=c(2,1,1,1),lwd=2,border="black",box.lwd=0 )
   

